package http;

import model.ApiConfigDataModel;
import model.ApiDataModel;
import model.ApiResponseDataModel;
import tool.DataReplace;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yangxianming on 2015-06-10.
 */
public class Http {
//    HttpURLConnection huc = null;
    ApiDataModel adm = null;
    ApiConfigDataModel acdm = null;

    String urlFull = null;  //组装完成的url路径 ip+port+path
    String method = null;
    boolean isMultipar = false;

    Http(ApiDataModel adm, ApiConfigDataModel acdm){
//        初始化就要获取method类型,post的两种类型
        this.adm = adm;
        this.acdm = acdm;
        this.method = adm.method;
        this.urlFull = adm.domain + adm.url;
        if (adm.header.get("Content-Type") != null)
            if (adm.header.get("Content-Type").contains("multipart"))
                isMultipar = true;

//        修改数据后放回对象
        adm.domain = DataReplace.replace(acdm, adm.domain);
        adm.url  = DataReplace.replace(acdm, adm.url);
    }

    public void httpDo() throws Exception{
        URL url;
        HttpURLConnection conn = null;
        ApiResponseDataModel ardm = new ApiResponseDataModel();
//        adm和acdm的原始数据在packageDataImpl接口已经处理,所以Header对象要在这里new并传入原始数据
        Header header = new Header(adm, acdm);
//        替换url路径中的变量
        urlFull = DataReplace.replace(acdm, urlFull);

        if (method.equals("get")){
//            设置数据
            PackageNormalData packageData = new PackageNormalData(adm, acdm);
//            get请求
//            urlFull = DataReplace.replace(acdm, urlFull);
            RequestGet get = new RequestGet(conn, urlFull, header);
            conn = get.sendData(packageData.packageData());
        }

        if (method.equals("put")){
//            设置数据
            PackageNormalData packageData = new PackageNormalData(adm, acdm);
//            get请求
//            urlFull = DataReplace.replace(acdm, urlFull);
            RequestPut put = new RequestPut(conn, urlFull, header);
            conn = put.sendData(packageData.packageData());
        }

        if (method.equals("post")){
            url = new URL(urlFull);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
//            conn.setInstanceFollowRedirects(false);
            header.setHeader(conn);

//            处理上传文件的post
            if (adm.header.containsKey("Content-Type") &&
                    adm.header.get("Content-Type").contains("multipart")){
                PackageMultipartData packageMultipartData = new PackageMultipartData(adm, acdm);
                OutputStream out = (OutputStream)packageMultipartData.packageData(conn.getOutputStream());
                RequestPost post = new RequestPost(conn, urlFull, header);
                post.sendData(out);
            }
//            处理普通的post
            if ((adm.header.containsKey("Content-Type") && !adm.header.get("Content-Type").contains("multipart")) ||
                    !adm.header.containsKey("Content-Type")){
                PackageNormalData packageData = new PackageNormalData(adm, acdm);
                RequestPost post = new RequestPost(conn, urlFull, header);
                post.sendData((String)packageData.packageData());
            }
        }

//        读取返回数据
        Response response = new Response(conn, adm, ardm, acdm);
        response.doOthersMatter();
        response.getResponseState();
        response.getResponseHeader();
        response.getResponseBody();
    }
}
